package com.lvxing.admin.sys.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lvxing.admin.sys.entity.School;
import com.lvxing.admin.sys.service.ISchoolService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 学校 前端控制器
 * </p>
 *
 * @author lvxing
 * @since 2019-07-21
 */
@RestController
@RequestMapping("/sys/school")
public class SchoolController {

    private static Logger logger = LoggerFactory.getLogger(SchoolController.class);

    @Autowired
    ISchoolService schoolService;

    @PostMapping("/addSchool")
    public String addSchool(@RequestBody School school) {
        boolean save = schoolService.save(school);
        return null;
    }

    @PostMapping("/removeSchoolById")
    public String removeSchoolById(String id) {
        boolean b = schoolService.removeById(id);
        return null;
    }

    @PostMapping("/editSchoolById")
    public String editSchoolById(@RequestBody School school) {
        boolean b = schoolService.updateById(school);
        return null;
    }

    @PostMapping("/getSchoolById")
    public String getSchoolById(String id) {
        School school = schoolService.getById(id);
        return null;
    }

    @PostMapping("/getSchoolList")
    public String getSchoolList(String search) {
        QueryWrapper<School> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(search),"school_name", search).or(qw -> qw.like(StringUtils.isNotEmpty(search),"school_address", search));
        queryWrapper.orderByDesc("update_time");
        IPage<School> page = new Page<School>();
        page.setPages(1);
        page.setSize(10);
        IPage<School> pageSchool = schoolService.page(page, queryWrapper);
        List<School> records = pageSchool.getRecords();
        logger.info(JSON.toJSONString(records));
        long total = pageSchool.getTotal();
        logger.info(JSON.toJSONString(total));
        return null;
    }

}
